package com.goods.service.impl;

import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.ProductStockMapper;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductStock;
import com.goods.common.vo.business.ProductStockVO;
import com.goods.common.vo.business.ProductVO;
import com.goods.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductStockMapper productStockMapper;

    @Override
    public List<Product> findProductList(String name, String categoryId, String supplier, Integer status) {
        if (StringUtils.isEmpty(status) || !StringUtils.isEmpty(name) || !StringUtils.isEmpty(categoryId) || !StringUtils.isEmpty(supplier)){
            String i1 = null;
            String i2 = null;
            String i3 = null;
            if (!StringUtils.isEmpty(categoryId)) {
                if (categoryId.indexOf(",") > 0){
                    String[] split = StringUtils.split(categoryId, ",");
                    i1 = split[0];
                    String i = split[1];
                    if (i.indexOf(",") > 0){
                        String[] split1 = StringUtils.split(i, ",");
                        i2 = split1[0];
                        i3 = split1[1];
                        return productMapper.selectByNameCategoryId(name,i1,i2,i3,status);
                    }
                    return productMapper.selectByNameCategoryId2(name,i1,i,status);
                }
                return productMapper.selectByNameCategoryId3(name,categoryId,status);
            }
        }

        return productMapper.selectAll();
    }

    @Override
    public int add(ProductVO productVO) {
        Product product = new Product();
        product.setName(productVO.getName());
        product.setPNum(UUID.randomUUID().toString());
        product.setModel(productVO.getModel());
        product.setUnit(productVO.getUnit());
        product.setRemark(productVO.getRemark());
        product.setSort(productVO.getSort());
        product.setCreateTime(new Date());
        product.setModifiedTime(new Date());
        product.setOneCategoryId(productVO.getCategoryKeys()[0]);
        product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
        product.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        product.setImageUrl(productVO.getImageUrl());
        product.setStatus(2);
        return productMapper.insert(product);
    }

    @Override
    public Product edit(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectOne(product);
        return product1;
    }

    @Override
    public int update(Long id, ProductVO productVO) {
        Product product = new Product();
        product.setId(id);
        product.setName(productVO.getName());
        product.setPNum(productVO.getPNum());
        product.setModel(productVO.getModel());
        product.setUnit(productVO.getUnit());
        product.setCreateTime(productVO.getCreateTime());
        product.setModifiedTime(new Date());
        product.setRemark(productVO.getRemark());
        product.setSort(productVO.getSort());
        product.setOneCategoryId(productVO.getCategoryKeys()[0]);
        product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
        product.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        product.setImageUrl(productVO.getImageUrl());
        product.setStatus(productVO.getStatus());
        int i = productMapper.updateByPrimaryKey(product);
        return i;
    }

    @Override
    public int remove(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectOne(product);
        product1.setStatus(1);
        int i = productMapper.updateByPrimaryKey(product1);
        return i;
    }

    @Override
    public int back(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectOne(product);
        product1.setStatus(0);
        int i = productMapper.updateByPrimaryKey(product1);
        return i;
    }

    @Override
    public int delete(Long id) {
        Product product = new Product();
        product.setId(id);
        int i = productMapper.delete(product);
        return i;
    }

    @Override
    public int publish(Long id) {
        Product product = new Product();
        product.setId(id);
        Product product1 = productMapper.selectOne(product);
        product1.setStatus(0);
        int i = productMapper.updateByPrimaryKey(product1);
        return i;
    }

    @Override
    public List<Product> findProducts(Integer status) {
        Product product = new Product();
        product.setStatus(status);
        return productMapper.select(product);
    }

    @Override
    public List<ProductStockVO> findProductStocks(String categorys,String name) {
        Product product = new Product();
        product.setStatus(0);
        List<Product> select = null;

        if (StringUtils.isEmpty(categorys) && StringUtils.isEmpty(name)){
            select = productMapper.select(product);
        }else {
            if (!StringUtils.isEmpty(categorys)){
                String[] split = StringUtils.split(categorys, ",");
                String c1Id = split[0];
                String[] split1 = StringUtils.split(split[1], ",");
                String c2Id = split1[0];
                String c3Id = split1[1];
                select = productMapper.selectByNameCategoryId(name,c1Id,c2Id,c3Id,0);
            }else {
                select = productMapper.selectByName(name);
            }
        }

        List<ProductStockVO> collect = select.stream().map(product1 -> {
            ProductStockVO productStockVO = new ProductStockVO();
            productStockVO.setId(product1.getId());
            productStockVO.setUnit(product1.getUnit());
            productStockVO.setName(product1.getName());
            productStockVO.setPNum(product1.getPNum());
            productStockVO.setModel(product1.getModel());
            productStockVO.setImageUrl(product1.getImageUrl());
            productStockVO.setRemark(product1.getRemark());
            ProductStock productStock = new ProductStock();
            productStock.setPNum(product1.getPNum());
            ProductStock productStock1 = productStockMapper.selectOne(productStock);
            productStockVO.setStock(productStock1.getStock());
            return productStockVO;
        }).collect(Collectors.toList());
        return collect;
    }

}
